import { Component, OnInit } from '@angular/core';

import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../../public/serve/hints/hints.service';
import { RemoveTableService } from '../../../../public/serve/remove-table/remove-table.service';
import { InitService } from '../../../../public/serve/init/init.service';
import { TreeNode } from '../../../../public/common/api';
import { Service } from '../../../../server/server';

// import { taskAddEimt } from "../../product-task-list";
import { appProductUrl } from '../../../product.url';
import { checktask } from '../../../product.data';
import { ActivatedRoute,Params, Router } from '@angular/router';
import {appSynonymUrl} from '../../../../synonym/synonym.url';
declare var $:any;

@Component({
    selector: 'app-product-indexing-task-audit-table',
    templateUrl: './product-indexing-task-audit-table.component.html',
    styleUrls: ['./product-indexing-task-audit-table.component.css']
})

export class ProductIndexingTaskAuditTableComponent implements OnInit {
    total: number = 0;
    naturalNum: number = 0;
    tabVal: Array<object>;
    files: TreeNode[];

    taskType: any;

    // isSelectAll: boolean = false;
    // isChose: boolean = false;
    isActive = this.server.isActive;
    right: any = {
        "taskPermissionAudit":{},
        "productAudit": {},
        "internalAudit": {},
        "themeTaskAudit":{}
    };

    constructor(
        private server: Service,
        public request: CudTableService,
        private tableListService: CudTableService,
        private hintsService: HintsService,
        public removeTableService: RemoveTableService,
        public initService: InitService,
        private activatedRoute: ActivatedRoute,
        private router: Router,
    ) { 
        this.right = $.extend(this.right, server.getRight());
    }

    ngOnInit() {
        this.server.eventEmit.subscribe((value: any) => {
            this.isActive = value;
        });
        this.activatedRoute.queryParams.subscribe((p: Params) => {
            this.taskType = String(p['taskType']);
            //权限
            if(this.taskType == '1'){
                //内文任务
                this.right['taskPermissionAudit'] = this.right['internalAudit'];
            }else if(this.taskType == '0'){
                //产品任务
                this.right['taskPermissionAudit'] = this.right['productAudit'];
            }else if(this.taskType == '2'){
                //主题词任务
                this.right['taskPermissionAudit'] = this.right['themeTaskAudit'];
            }

            this.getTabList();
        });
    }

    getTabList() {
        this.files = [];
        let params = this.getParams();
        this.request.postRequest(this.server.baseUrl +appProductUrl.getCheckTasksList, params).then((res: any) => {
            if (res.result_code === '0000') {
                this.tabVal = res.result;
                this.total = res.length;
                this.naturalNum = checktask.pagination.naturalNum;
                if (res.length > 0) {
                    this.files = this._processTreeData(this.tabVal, 'taskId', 'parentid', 'name');
                }
            }
            return;
        }).catch((err: any) => {
            this.hintsService.messages("error", "error");
            return;
        });
    }

    //查询参数
    private getParams() {
        return {
            token: checktask.retrieval.token,
            pageNo: checktask.pagination.pageNo,
            pageSize: checktask.pagination.pageSize,
            queryCriteria: checktask.retrieval.queryCriteria,
            checkStatus: checktask.retrieval.checkStatus,
            taskCheckBy: this.server.getuserName(),
            id: checktask.retrieval.id,
            taskType: this.taskType,
            parentId: ""
        }
    }

    _processTreeData(list: Array<any>, idName, parentIdName, labelName): Array<any> {
        return this._toTree(list, '0', idName, parentIdName, labelName);
    }

    _toTree(list: Array<any>, topId = '0', idName, parentIdName, labelName) {
        let idKey = idName;
        let result = [];
        for (let item of list) {
            if (item[parentIdName] == topId) {
                let children = {};
                let leaf = false;
                if (item['child'] == '0') {
                    leaf = true;
                }
                let dataArr = {
                    'data': item,
                    'taskId': item['taskId'],
                    'leaf': leaf
                };
                result.push(dataArr);
            }
        }
        return result;
    }

    //树形子节点
    onNodeExpand(event) {
        const node = event.node;
        let queryData = {
            token: checktask.retrieval.token,
            pageNo: 1,
            pageSize: 9999,
            queryCriteria: checktask.retrieval.queryCriteria,
            checkStatus: checktask.retrieval.checkStatus,
            taskCheckBy: this.server.getuserName(),
            taskType: this.taskType,
            parentId: node.data.taskId
        };
        this.getChildrenList(queryData, node);
    }

    /**
     * 点击图标显示子级的任务
     * @param queryData 
     * @param node 
     */
    public getChildrenList(queryData, node) {
        this.request.postRequest(this.server.baseUrl +appProductUrl.getCheckTasksList, queryData).then((data: any) => {
            if (data.result_code == '0000') {
                node['children'] = this._toTree(data.result, node['taskId'], 'taskId', 'parentid', 'name');
                this.files = [...this.files];
                return false;
            } else {
                this.hintsService.messages(data.message, data.result_code);
            }

        }).catch();
    }

    // /**
    //  * 全选
    //  * @param bool
    //  * @param tabVal
    //  */
    // _selectAll(bool, tabVal) {
    //     this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'taskId');
    // }

    // /**
    //  * 单选
    //  * @param bool
    //  * @param taskId
    //  * @param length
    //  */
    // _chose(bool, taskId, length) {
    //     this.isSelectAll = this.removeTableService.removeChose(bool, taskId, length)['bool'];
    // }

    // /**
    //  * 重置表格复选框、选中的ID
    //  */
    // private _cancelChecked() {
    //     this.isChose = this.isSelectAll = false;
    //     this.removeTableService._clearId('taskId');
    // }
}

